#pragma once
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_map>
using namespace std;

string src_root="./root/html";
string space = " ";
string line_break = "\r\n";
string kv_delimiter = ": ";

class HttpRequestParser
{
public:
    HttpRequestParser(const std::string &request) : request_(request)
    {
        parse();
    }

    std::string getMethod() const
    {
        return method_;
    }

    std::string getPath() const
    {
        return path_;
    }

    std::string getHeader(const std::string &key) const
    {
        auto it = headers_.find(key);
        if (it != headers_.end())
        {
            return it->second;
        }
        return "";
    }

    std::string getBody() const
    {
        return body_;
    }

private:
    void parse()
    {
        std::istringstream requestStream(request_);
        std::string line;

        // 解析第一行（请求行）
        if (std::getline(requestStream, line))
        {
            std::istringstream lineStream(line);
            lineStream >> method_ >> path_ >> httpVersion_;
        }

        // 解析头部信息
        while (std::getline(requestStream, line) && line != "\r")
        {
            size_t colonPos = line.find(':');
            if (colonPos != std::string::npos)
            {
                std::string key = line.substr(0, colonPos);
                std::string value = line.substr(colonPos + 1);
                // 去除前后空格
                value.erase(0, value.find_first_not_of(" \t"));
                value.erase(value.find_last_not_of(" \t\r") + 1);
                headers_[key] = value;
            }
        }

        // 解析请求体
        std::getline(requestStream, body_, '\0');
    }

    std::string request_;
    std::string method_;
    std::string path_;
    std::string httpVersion_;
    std::unordered_map<std::string, std::string> headers_;
    std::string body_;
};

class Response
{
    void GetContent()
    {
        _body_line.clear();
        std::ifstream in(_response_url, std::ios::binary);
        if (!in.is_open())
            return;
        in.seekg(0, in.end);
        int filesize = in.tellg();
        in.seekg(0, in.beg);

        _body_line.resize(filesize);
        in.read((char *)_body_line.c_str(), filesize);
        in.close();
    }

public:
    Response(HttpRequestParser &request)
    {
        _statu_line = "HTTP/2 200 OK";
        _header_line = "Content-Type: text/html; charset=UTF-8" + line_break;
        _body_line = "";
        _response_url = src_root + request.getPath();
        if(_response_url==src_root+"/")
        {
            _response_url = src_root + "/index.html";
        }
        if(_response_url.find(".css")!=string::npos)
        {
            _header_line = "Content-Type: text/css; charset=UTF-8" + line_break;
        }
        GetContent();
    }

    void Getindex_html(string &content)
    {
        string index;
        index += (_statu_line + line_break);
        // index+=(_header_line+line_break);
        index+=line_break;
        index += _body_line;
        content = index;
    }

private:
    string _statu_line;
    string _header_line;
    string _body_line;

    string _response_url;
};